//
//  UtilsMacros.h
//  banShopping
//
//  Created by Summer on 2020/8/27.
//  Copyright © 2020 Summer. All rights reserved.
//

#ifndef UtilsMacros_h
#define UtilsMacros_h


//=======================================================//

//MARK: 用户信息
#define UserDefaults [NSUserDefaults standardUserDefaults]
#define kEmail        @"email"
#define kNickName     @"nickName"
#define kPassword     @"password"
#define kCountry      @"country"      //China
#define kRegionCode   @"regionCode"   //530000000
#define kAreacode     @"areacode"     //CN HK  US
#define kIsLogined    @"kIsLogined"
#define kToken        @"token"
#define kServerApi    @"serverApi"
#define kBrandID      @"brandId"    //
#define kWiFiSSID     @"WiFiSSID"
#define kWiFiPWD      @"WiFiPWD"
#define kIsRememberWiFi      @"isRememberWiFi"
#define kLanguage     @"ruihe_language"
//=======================================================//

//MARK: Empty  无数据的默认图
#define ListEmpty(imageName,title) \
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{return kImage((imageName));}\
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{return YES;}\
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{NSString *text = (title);NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:16.0f],NSForegroundColorAttributeName: UIColorFromRGB(0x787878)};return [[NSAttributedString alloc] initWithString:text attributes:attributes];}

//=======================================================//

//MARK: 首页轮播图高度
#define kBannerHeightValue   (kUI_WINDOW_W*(360/750.0f))

//======================================================//
#pragma mark - Bundle名称 和 路径
#define  MBPROGRESS_BUNDLE_NAME   @ "MBProgressHUD.bundle"
#define  MBPROGRESS_BUNDLE_PATH   [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MBPROGRESS_BUNDLE_NAME]
#define  MBPROGRESS_BUNDLE        [NSBundle bundleWithPath: MBPROGRESS_BUNDLE_PATH]

#define  WEATHER_BUNDLE_NAME      @ "Weather.bundle"
#define  WEATHER_BUNDLE_PATH      [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: WEATHER_BUNDLE_NAME]
#define  WEATHER_BUNDLE           [NSBundle bundleWithPath: WEATHER_BUNDLE_PATH]
//=============================//

//MARK: Documents 路径
#define KFBX_TEMPPATH(name, postfix) [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@.%@",name,postfix]]

//=======================================================//

//MARK: 左边露出的宽度（用于判断拖动手势）
//#define kLeftSlideDistance   (kUI_WINDOW_W*0.2)
#define kLeftSlideDistance   0 //移除左侧滑，重置宽度为0

//MARK: 获取系统对象
#define kApplication         [UIApplication sharedApplication]
#define kAppWindow           kApplication.delegate.window
#define kShareAppDelegate    [AppDelegate shareAppDelegate]
#define kRootViewController  kAppWindow.rootViewController
#define kUserDefaults        [NSUserDefaults standardUserDefaults]
#define kNotificationCenter  [NSNotificationCenter defaultCenter]
#define kbundleIdentifier [[NSBundle mainBundle] bundleIdentifier]

//=======================================================//
#pragma mark - APP版本号
#define  kVersion_App      [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]
#define  kVersionBuild_App [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]

//=======================================================//
//MARK: 获取屏幕宽高
#define kUI_WINDOW_B   [UIScreen mainScreen].bounds
#define kUI_WINDOW_W   kUI_WINDOW_B.size.width
#define kUI_WINDOW_H   kUI_WINDOW_B.size.height

//MARK: 根据ip5的屏幕来拉伸
#define kScaleFromIp5(_X_)   (_X_ * (kUI_WINDOW_W/320.0f))

#define Iphone7ScaleWidth    kUI_WINDOW_W/375.0
#define Iphone7ScaleHeight   kUI_WINDOW_W/667.0
//MARK: 根据ip7的屏幕来拉伸
#define kRealValue(with)     ((with)*(Iphone7ScaleWidth))
#define kFontScale           Iphone7ScaleWidth

#pragma mark - 系统UI
#define  kNavBarHeight   44
#define  kStatusHeight   20
#define  kTopBarHeight   64
#define  kToolBarHeight  49
#define  kBottomToolBar  34  //iPhoneX底栏安全距离  比其他 iPhone 高34
#define  kBottomZero     0

//MARK: 状态栏高度
#define  kStatusBarHeight          [kApplication statusBarFrame].size.height
#define  kSafe_top                 [UIView getSafeInset_top]
#define  kSafe_bottom              [UIView getSafeInset_bottom]
//导航栏高度
#define kNavHeight       [UIView getNavHeight]
//tabbar高度
#define kTabbarHeight    [UIView getTabbarHeight]
#define kScaleW(x)                 [UIView getScaleWidth:x]
//MARK: 判断是否为 iPhoneX，iPhoneX的状态栏高度 为 40， 其他iPhone为 20
//#define  KKiPhoneX                 (kStatusBarHeight>kStatusHeight ? YES : NO)

//MARK: 判断是否iPhoneX以上设备 1:是   0:否
#define KKiPhoneX ({\
int tmp = 0;\
if (@available(iOS 11.0, *)) {\
UIWindow * window = [[[UIApplication sharedApplication] delegate] window];\
if (window.safeAreaInsets.bottom > 0.0) {\
tmp = 1;\
}else{\
tmp = 0;\
}\
}else{\
tmp = 0;\
}\
tmp;\
})

//MARK: 适配iPhoneX NavigationBar高度
#define  kTopNavigationBarHeight   (KKiPhoneX ?(kStatusBarHeight+kNavBarHeight) : kTopBarHeight)

//MARK: 适配iPhoneX 底栏TabBar高度
#define  kTabBarHeight             (KKiPhoneX ? (kToolBarHeight+kBottomToolBar) : kToolBarHeight)

//MARK: 适配iPhoneX StatusBar安全距离   （iPhoneX 顶部安全距离为 20.0f）
#define  kStatusBarSafeMargin       (KKiPhoneX ? kStatusHeight : kBottomZero)

//MARK: View底部 到 Tabbar底部的 安全距离 （iPhoneX 底栏安全距离为 34.0f）
#define kTabbarSafeBottomMargin    (KKiPhoneX ? kBottomToolBar : kBottomZero)

//MARK: iPhoneX的安全内边距， iPhoneX的顶部距离，因为iPhoneX的刘海 多20，底部上滑指示条 多34
#define KViewSafeAreInsets(view)   ({UIEdgeInsets insets; if(@available(iOS 11.0, *)) {insets = view.safeAreaInsets;} else {insets = UIEdgeInsetsZero;} insets;})

//=======================================================//
//判断是否是ipad
#define kDevice_Is_iPad ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
//MARK: 手机类型
//判断iPhone4系列
#define kDevice_Is_iPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) && !kDevice_Is_iPad : NO)
//判断iPhone5系列
#define kDevice_Is_iPhoneSE ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) && !kDevice_Is_iPad : NO)
//判断iPhone6系列
#define kDevice_Is_iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) && !kDevice_Is_iPad : NO)
//判断iphone6+系列
#define kDevice_Is_iPhone6p ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) && !kDevice_Is_iPad : NO)
//判断iPhoneX
#define kDevice_Is_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) && !kDevice_Is_iPad : NO)
//判断iPHoneXr
#define kDevice_Is_iPhoneXr ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size) && !kDevice_Is_iPad : NO)
//判断iPhoneXs
#define kDevice_Is_iPhoneXs ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) && !kDevice_Is_iPad : NO)
//判断iPhoneXs Max
#define kDevice_Is_iPhoneXs_Max ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size) && !kDevice_Is_iPad : NO)

//=======================================================//
//MARK: 强弱引用
#define kWeakSelf  __weak __typeof(self) weakSelf = self
#define kStrongSelf  __strong typeof(weakSelf) strongSelf = weakSelf

//=======================================================//
//MARK: View 圆角和加边框
#define ViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

//MARK:  View 圆角
#define ViewRadius(View, Radius)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES]

//=======================================================//
#define PropertyString(s)           @property(nonatomic, copy) NSString *s
#define PropertyNSInteger(s)        @property(nonatomic, assign) NSInteger s
#define PropertyFloat(s)            @property(nonatomic, assign) float s
#define PropertyLongLong(s)         @property(nonatomic, assign) long long s
#define PropertyNSDictionary(s)     @property(nonatomic, strong) NSDictionary *s
#define PropertyNSArray(s)          @property(nonatomic, strong) NSArray *s
#define PropertyNSMutableArray(s)   @property(nonatomic, strong) NSMutableArray *s
//=======================================================//
//MARK: IOS 版本判断
#define IOSAVAILABLEVERSION(version) ([[UIDevice currentDevice] availableVersion:version] < 0)
//MARK: 当前系统版本
#define CurrentSystemVersion [[UIDevice currentDevice].systemVersion doubleValue]
//MARK: 当前语言
#define CurrentLanguage (［NSLocale preferredLanguages] objectAtIndex:0])
//MARK: 判断是否是IOS11系统
#define IOS11_OR_LATER (CurrentSystemVersion >= 11.0)
//MARK: 判断设备的操作系统
#define IOS7  ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 ? YES : NO)
//MARK: 判断是否是IOS8的系统
#define IOS8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

//MARK: 判断是否是IOS9的系统
#define IOS9_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)

//=======================//
//MARK: 判断是否是IOS10系统
#define IOS10_OR_LATER (CurrentSystemVersion >= 10.0)

//=======================================================//
//MARK: DEBUG  模式下打印日志,当前行
#ifdef DEBUG
//Xcode8 NSLog打印json不全解决办法
#define NSLog(format, ...) printf(" class:< %s: (%d) > method: %s \n %s \n ", [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __PRETTY_FUNCTION__, [[NSString stringWithFormat:(format), ##__VA_ARGS__] UTF8String]);

#else
#define NSLog(...)

#endif
//=======================================================//
//MARK: 精简调用
#define kButton              [UIButton buttonWithType:UIButtonTypeCustom]
#define kLab                 [[UILabel alloc] init]
#define kView                [[UIView alloc] init]
#define URL(urlName)         [NSURL URLWithString:(urlName)]
#define kImageView           [[UIImageView alloc]init]
#define kTextField           [[UITextField alloc]init]
#define kImage(imageName)    [UIImage imageNamed:(imageName)]
//=======================================================//
#pragma mark -  定义UIImage对象
#define kImageWithFile(_pointer) [UIImage imageWithContentsOfFile:([[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@%dx", _pointer, (int)[UIScreen mainScreen].nativeScale] ofType:@"png"])]
#define kImageName(name) [UIImage imageNamed: name]

//=======================================================//
//MARK: 数据验证
#define StrValid(f)        (f!=nil && [f isKindOfClass:[NSString class]] && ![f isEqualToString:@""])
#define SafeStr(f)         (StrValid(f) ? f:@"")
#define HasString(str,key) ([str rangeOfString:key].location!=NSNotFound)

#define ValidStr(f)        StrValid(f)
#define ValidDict(f)       ([f isKindOfClass:[NSNull class]]? NO : (f!=nil && [f isKindOfClass:[NSDictionary class]]))
#define ValidArray(f)      (f!=nil && [f isKindOfClass:[NSArray class]] && [f count]>0)
#define ValidNum(f)        (f!=nil && [f isKindOfClass:[NSNumber class]])
#define ValidClass(f,cls)  (f!=nil && [f isKindOfClass:[cls class]])
#define ValidData(f)       (f!=nil && [f isKindOfClass:[NSData class]])
#define ValidNullData(data) [NSString checkNull:data defaultValue:@""]
#define ValidNullDataToString(data,toStr) [NSString checkNull:data defaultValue:toStr]
//=======================================================//

//MARK: 获取一段时间间隔
#define kStartTime CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#define kEndTime  NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)
#pragma mark -  打印当前方法名
#define ITTDPRINTMETHODNAME() ITTDPRINT(@"%s", __PRETTY_FUNCTION__)

//MARK: 延时处理，适配iOS2快速唤起APP，网络中断问题
#define delayTimeIos12Later  0.7

//=======================================================//
//MARK:系统通知 ----
// 注册通知
#define kNOTIFY_ADD(_noParamsFunc, _notifyName)  [[NSNotificationCenter defaultCenter] \
addObserver:self \
selector:@selector(_noParamsFunc) \
name:_notifyName \
object:nil];

// 发送通知
#define kNOTIFY_POST(_notifyName, _object)   [[NSNotificationCenter defaultCenter] postNotificationName:_notifyName object: _object];

// 移除通知
#define kNOTIFY_REMOVE(_notifyName, _object) [[NSNotificationCenter defaultCenter] removeObserver:self name:_notifyName object: _object];


/** notificationName */
#define IsEnablePersonalCenterVCMainTableViewScroll @"IsEnablePersonalCenterVCMainTableViewScroll"
//#define CurrentSelectedChildViewControllerIndex @"CurrentSelectedChildViewControllerIndex"
#define PersonalCenterVCBackingStatus @"PersonalCenterVCBackingStatus"
#define SegementViewChildVCBackToTop @"segementViewChildVCBackToTop"

/********** 状态栏高度 **********/
#define NAVIGATIONBARHEIGHT  [[UIApplication sharedApplication] statusBarFrame].size.height
/********** 导航栏高度 **********/
#define STATUSBARHEIGHT    44
/********** 导航栏+状态栏高度 **********/
#define NAVHEIGHT       (NAVIGATIONBARHEIGHT+STATUSBARHEIGHT)

//=======================================================//

//MARK:提示弹窗   例如： alert(@"请填写内容", nil, @"确定", nil);
#define alert(aMessage,isSelf,aCencel,aOther) [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:aMessage delegate:isSelf cancelButtonTitle:aCencel otherButtonTitles:aOther, nil] show]

//=======================================================//
//MARK:-  GCD
#define kDISPATCH_ASYNC_BLOCK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),block)
#define kDISPATCH_MAIN_BLOCK(block) dispatch_async(dispatch_get_main_queue(),block)

//MARK:GCD -一次性执行
#define kDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken;dispatch_once(&onceToken,onceBlock);

//MARK:单例化一个类
#define SINGLETON_FOR_HEADER(className) \
\
+ (className *)shared##className;

#define SINGLETON_FOR_CLASS(className) \
\
+ (className *)shared##className { \
static className *shared##className = nil; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
shared##className = [[self alloc] init]; \
}); \
return shared##className; \
}

//MARK: 常用字符串方法
static inline NSString *FBXStringFormInteger(NSInteger integer) {
    return [NSString stringWithFormat:@"%li", integer];
}
static inline NSString *FBXStringFormFloat(CGFloat f) {
    return [NSString stringWithFormat:@"%lf", f];
}
static inline NSString *FBXStringFormDouble(CGFloat f) {
    return [NSString stringWithFormat:@"%lf", f];
}
static inline NSMutableAttributedString *FBXAddAttributeString(NSString *string, NSDictionary<NSString *, id> *att, NSRange range) {
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
    [attString addAttributes:att range:range];
    return attString;
}
static inline NSURL *FBXURLFormString(NSString *s) {
    return [NSURL URLWithString:s];
}
static inline NSString *FBXJSONStringFormArray(NSArray *array) {
    NSData *ctgIdData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:nil];
    NSString *ctgIdStr =  [[NSString alloc] initWithData:ctgIdData encoding:NSUTF8StringEncoding];
    return ctgIdStr;
}

static inline NSString *FBXStringWithFormat(NSString *formatString, ...) {
    va_list args;
    va_start(args, formatString);
    NSString *string = [[NSString alloc] initWithFormat:formatString arguments:args];
    va_end(args);
    
#if defined(__has_feature) && __has_feature(objc_arc)
    return string;
#else
    return [string autorelease];
#endif
}
static inline CGSize FBXSizeOfString(NSString * text, CGSize maxSize, UIFont * font) {
    CGSize fitSize;
    
#ifdef __IPHONE_7_0
    fitSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil].size;
#else
    fitSize = [text sizeWithFont:font constrainedToSize:maxSize];
#endif
    return fitSize;
}


#endif /* UtilsMacros_h */
